package cn.kai.basic.controller;

import cn.kai.basic.dto.RegisterDto;
import cn.kai.basic.exception.BusinessException;
import cn.kai.basic.service.IVerifyCodeService;
import cn.kai.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Kai
 * @Date 2022/6/15 19:27
 * @PackageName:cn.kai.basic.controller
 * @ClassName: VerifyCodeController
 * @Description: 生成验证码接口
 * @Version 1.0
 */

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key){
        try {
            String base64Str = verifyCodeService.imageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 发生短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

}
